/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.logistics.util.express.yto;

import cn.hutool.core.date.DateUtil;
import com.medusa.gruul.common.core.util.StringUtil;
import com.medusa.gruul.logistics.util.HttpClientUtil;
import com.medusa.gruul.logistics.util.VerifyCodeUtil;
import com.medusa.gruul.logistics.util.CoverXMLUtil;
import com.medusa.gruul.logistics.util.express.yto.bean.Receiver;
import com.medusa.gruul.logistics.util.express.yto.bean.RequestOrder;
import com.medusa.gruul.logistics.util.express.yto.bean.Sender;
import com.medusa.gruul.logistics.util.express.yto.bean.route.Result;
import com.medusa.gruul.logistics.util.express.yto.bean.route.Ufinterface;
import com.medusa.gruul.logistics.util.express.yto.bean.route.WaybillCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * <p>
 * 快递接口工具类
 * </p>
 *
 * @author lcysike
 * @since 2020-03-10
 */
public class YTOExpressUtil {

    public static Logger logger = LoggerFactory.getLogger(YTOExpressUtil.class);


    /**
     * 快递下单接口
     */
    private static final String REQ_URL = "http://opentestapi.yto.net.cn/service/order_create/v1/G7v1Kn";

    /**
     * 快递物流查询接口
     */
    private static final String ROUTE_URL = "http://opentestapi.yto.net.cn/service/waybill_query/v1/G7v1Kn";

    /**
     * 物流查询顾客编码
     */
    private static final String ROUTE_CLIENT_CODE = "YTOTEST";
    /**
     * 物流查询校验码
     */
    private static final String ROUTE_CHECK_WORD = "1QLlIZ";

    /**
     * 下单顾客编码
     */
    private static final String CLIENT_CODE = "TEST";
    /**
     * 下单校验码
     */
    private static final String CHECK_WORD = "123456";
    /**
     * 编码格式
     */
    private static final String UNION_CODE = "UTF-8";
    /**
     * 语言
     */
    private static final String LANG = "zh-CN";

    /**
     * 生成顺丰快递单号
     *
     * @param orderNo
     * @return
     */
    public static String getExpressNo(String orderNo) {

        SimpleDateFormat sdfDate3 = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        RequestOrder requestOrder = new RequestOrder();
        requestOrder.setClientID(CLIENT_CODE);
        requestOrder.setLogisticProviderID("YTO");
        requestOrder.setTxLogisticID(orderNo);
        requestOrder.setOrderType(1);
        requestOrder.setServiceType(1L);
        requestOrder.setSpecial(0);
        Sender sender = new Sender();
        sender.setName("李晨阳");
        sender.setPostCode("315000");
        sender.setMobile("13566035521");
        sender.setProv("浙江省");
        sender.setCity("宁波市");
        sender.setAddress("浙江省宁波市高新区");
        requestOrder.setSender(sender);
        Receiver receiver = new Receiver();
        receiver.setName("李晨阳");
        receiver.setPostCode("315000");
        receiver.setMobile("13566035521");
        receiver.setProv("浙江省");
        receiver.setCity("宁波市");
        receiver.setAddress("浙江省宁波市高新区");
        requestOrder.setReceiver(receiver);
        String reqXml = CoverXMLUtil.convertToXml(requestOrder, UNION_CODE);
        logger.info("请求报文:" + reqXml);
        String respXml = callYTOExpressServiceByCSIM(REQ_URL, reqXml, CLIENT_CODE);
        if (respXml != null) {
            logger.info("--------------------------------------");
            logger.info("圆通在线下单返回报文: " + respXml);
            logger.info("--------------------------------------");
        }
        return respXml;
    }

    /**
     * 获取顺丰快递单号路由信息
     *
     * @param orderNo
     * @return
     */
    public static String getYTORoute(String orderNo) {
        //组装请求信息
        Ufinterface ufinterface = new Ufinterface();
        Result result = new Result();
        WaybillCode waybillCode = new WaybillCode();
        waybillCode.setNumber(orderNo);
        result.setWaybillCode(waybillCode);
        ufinterface.setResult(result);
        String reqXml = CoverXMLUtil.convertToXml(ufinterface, UNION_CODE);
        System.out.println("请求报文:" + reqXml);
        String appKey = "sF1Jzn" ;
        String format = "XML" ;
        String method = "yto.Marketing.WaybillTrace" ;
        String timestamp = DateUtil.now();
        String userId =  ROUTE_CLIENT_CODE;
        String v = "1";
        String sign = ROUTE_CHECK_WORD + "app_key"+ appKey + "format" + format + "method" + method + "timestamp" + timestamp + "user_id" + userId + "v" + v;
        sign = StringUtil.upperCase(VerifyCodeUtil.encrypt32(sign));
        String respXml = queryAPIRouteService(ROUTE_URL, sign, appKey, format, method, timestamp, userId, v, reqXml);
        if (respXml != null) {
            logger.info("--------------------------------------");
            logger.info("圆通物流查询返回报文: " + respXml);
            logger.info("--------------------------------------");
        }
        return respXml;
    }

    public static String callYTOExpressServiceByCSIM(String reqURL, String reqXML, String clientCode) {
        String result = null;
        String verifyCode = VerifyCodeUtil.md5EncryptAndBase64(reqXML + CHECK_WORD);
        result = queryAPIservice(reqURL, reqXML, verifyCode);
        return result;
    }

    public static String queryAPIRouteService(String routeURL, String sign, String appKey, String format, String method, String timestamp, String userId, String v, String reqXml) {
        HttpClientUtil httpclient = new HttpClientUtil();
        if (routeURL == null) {
            routeURL = REQ_URL;
        }
        String result;
        try {
            result = httpclient.postYTORouteAPI(routeURL, sign, appKey, format, method, timestamp, userId, v, reqXml);
            return result;
        } catch (Exception var6) {
            logger.warn(" " + var6);
            return null;
        }
    }

    public static String queryAPIservice(String url, String xml, String verifyCode) {
        HttpClientUtil httpclient = new HttpClientUtil();
        if (url == null) {
            url = ROUTE_URL;
        }
        String result;
        try {
            result = httpclient.postYTOAPI(url, xml, verifyCode, CLIENT_CODE, "online");
            return result;
        } catch (Exception var6) {
            logger.warn(" " + var6);
            return null;
        }
    }

//    public static void main(String[] args) {
//        //String response = getExpressNo("1018370195865");
//        String response = getYTORoute("YT4389115343241");
//        System.out.println(response);
//    }

}